import config from '@/config'

export const useGlobalStore = defineStore('globalStore', {
    state: () => ({
        userInfo: null as any, // 用户信息
        mapLoaded: false,
        regionCode: config.regionCode, // 区域编码
        regionName: config.regionName, // 区域名称
        treeId: config.treeId, // 区域id
        gridCode: config.gridCode, // 网格编码
        gridName: config.gridName, // 网格名称
        currentTool: '', // 当前使用的工具条工具
        currentPopName: '', // 当前展开的普通弹窗名称
        currentPopData: null as any, // 当前展开的普通弹窗传递的数据
        screenRatio16_9: true,
        showPageLeft: true,
        showPageRight: true,
    }),
    actions: {
        changeUserInfo(data: any) {
            this.userInfo = data
        },
        changeMapLoaded(data: any) {
            this.mapLoaded = data
        },
        changeRegionAndGrid(data: any) {
            this.regionCode = data.regionCode
            this.regionName = data.regionName
            this.treeId = data.treeId
            this.gridCode = data.gridCode
            this.gridName = data.gridName
        },
        resetRegionAndGrid() {
            this.regionCode = config.regionCode
            this.regionName = config.regionName
            this.treeId = config.treeId
            this.gridCode = config.gridCode
            this.gridName = config.gridName
        },
        changeCurrentTool(data: any) {
            this.currentTool = data
        },
        changePopInfo(data: { popName: string; popData?: any }) {
            this.currentPopName = data.popName
            this.currentPopData = data.popData
        },
        clearPopInfo() {
            this.currentPopName = ''
            this.currentPopData = null
        },
        togglePopInfo(data: { popName: string; popData?: any }) {
            if (data.popName) {
                if (data.popName === this.currentPopName) {
                    this.clearPopInfo()
                } else {
                    this.clearPopInfo()
                    nextTick(() => {
                        this.changePopInfo(data)
                    })
                }
            } else {
                this.clearPopInfo()
            }
        },
        changeScreenRatio16_9(state: boolean) {
            this.screenRatio16_9 = state
        },
        changePageLeft(state: boolean) {
            this.showPageLeft = state
        },
        changePageRight(state: boolean) {
            this.showPageRight = state
        }
    }
})
